package com.example.qxfw.mongodb.qx.service.impl;

import com.example.qxfw.mongodb.qx.entity.QxLdPngFile;
import com.example.qxfw.mongodb.qx.service.QxLdPngFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/************************************
 *
 * @author : HuangDeYin Jolly
 * @since : JDK 11
 * @Date : 2023/8/10  星期四   15:11
 * @description : 此类用来
 ***************************************/
@Slf4j
@Service
public class QxLdPngFileServiceImpl implements QxLdPngFileService {

    @Qualifier("mongoPrimaryTemplate")
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<QxLdPngFile> findByDateCharAndTimeCharAndStationIdAndElevationAndLdTypeAndDateYear(final String datechar, final String timechar, final String stationid, final String elevation, final String ldtype, final String dateyear) {

        List<String> fieldsToSelect = Arrays.asList("_id", "datechar", "timechar");
        Criteria criteria = Criteria
                .where("datechar").is(datechar)
                .and("timechar").is(timechar)
                .and("stationid").is(stationid)
                .and("elevation").is(elevation)
                .and("ldtype").is(ldtype)
                .and("dateyear").is(dateyear);

        Query query = new Query(criteria);

        for (String field : fieldsToSelect) {
            query.fields().include(field);
        }
        return mongoTemplate.find(query, QxLdPngFile.class);
    }

    @Override
    public QxLdPngFile selectById(final String _id) {
        List<String> fieldsToSelect = Collections.singletonList("data");
        Criteria criteria = Criteria.where("_id").is(_id);
        Query query = new Query(criteria);
        for (String field : fieldsToSelect) {
            query.fields().include(field);
        }
        return mongoTemplate.findOne(query, QxLdPngFile.class);
    }
}
